<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2019/1/09/0009
 * Time: 17:11
 */

namespace app\sports_admin\service;


use app\exception\VideoTypeException;
use think\Request;
use app\sports_admin\model\VideoType as VideoTypeModel;
class VideoType
{
    /*
     * 添加子类信息
     */
    public static function insertChildren()
    {
        // path 路径  parent_id 父类id title 子类名称
        $params = Request::instance()->only(['path','parent_id','title']);
        $bool = VideoTypeModel::insertTypeInfo($params);
        if(!$bool){
            throw new VideoTypeException([
                'msg'=>'类别信息添加失败',
                'errorCode'=>400
            ]);
        }
        return json(['msg'=>'添加成功','code'=>200]);
    }
    /*
     * 添加父类信息
     */
    public static function insertParent()
    {
        // path 路径  parent_id 父类id title 子类名称
        $params = Request::instance()->only(['path','parent_id','title']);
        $bool = VideoTypeModel::insertTypeInfo($params);
        if(!$bool){
            throw new VideoTypeException([
                'msg'=>'类别信息添加失败',
                'errorCode'=>400
            ]);
        }
        return json(['msg'=>'添加成功','code'=>200]);
    }
    /*
     * 修改类别信息
     */
    public static function update()
    {
        // id 类别id title 类别名称
        $params = Request::instance()->only(['id','title']);
        $bool = (new VideoTypeModel())->save(['title'=>$params['title']],['id'=>$params['id']]);
        if(!$bool){
            throw new VideoTypeException([
                'msg'=>'类别信息修改失败',
                'errorCode'=>400
            ]);
        }
        return json(['msg'=>'修改成功','code'=>200]);
    }
    /*
     * 删除类别信息
     * @param $id 类别信息id
     */
    public static function deleteTypeInfo()
    {
        $id = input('post.id');
        $childType = VideoTypeModel::getTypeChild($id);
        if($childType){
            throw new VideoTypeException([
                'msg' => '不能删除含有子类的类别',
                'code' => 202]);
        }
        $bool = VideoTypeModel::destroy($id);
        if(!$bool){
            throw new VideoTypeException([
                'msg'=>'删除失败',
                'errorCode'=>400
            ]);
        }
        return json(['msg'=>'删除成功','code'=>200]);
    }
}